package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.OrderDetail;
import com.tjetc.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/orderdetail")
public class OrderDetailController {

    @Autowired
    private OrderDetailService orderDetailService;

    /***
     * 根据订单id查询所有订单详情
     * @param id
     * @return
     */
    @RequestMapping("all/{id}")
    public JsonResult queryAllByOrderId(@PathVariable int id){
        List<OrderDetail> orderDetails = orderDetailService.selectByOrderId(id);
        if(orderDetails!=null){
            return new JsonResult(0,"查询成功",orderDetails);
        }else {
            return new JsonResult(1,"查询失败",orderDetails);
        }
    }

    /***
     * 后端计算订单金额
     * @param list
     * @return
     */
    @RequestMapping("computed")
    public JsonResult computedAmount(List<OrderDetail> list){
        BigDecimal amount= orderDetailService.computed(list);
        return new JsonResult(0,"计算成功",amount);
    }

}
